//
//  SYDCusAnnotationView.m
//  PinGJByOwnerSide
//
//  Created by lantaiyuan on 2017/8/17.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//
//  自定义大头针
#import "SYDCusAnnotationView.h"
#import "CustomCalloutView.h"
#import "YMUserPointAnnotation.h"

#define kWidth  120.f
#define kHeight 60.f

#define kHoriMargin 5.f
#define kVertMargin 5.f

#define kPortraitWidth  50.f
#define kPortraitHeight 50.f

#define kFontSize 11.0  // 泡泡文字字体大小
#define kDiameter 20.0  // 标注点直径

@interface SYDCusAnnotationView ()

@property (nonatomic, strong) UIImageView *portraitImageView;

@property(nonatomic,strong)UIView *showView;

@property (strong,nonatomic)UILabel *timeStatusLabel;//时间

@property (strong,nonatomic)UIImageView *separateImageView;//分割

@property (strong,nonatomic)UILabel *locationLabel;//地点

@property (strong,nonatomic)UILabel *numberLabel;//上下车人数

@end
@implementation SYDCusAnnotationView
@synthesize calloutView;
@synthesize showView = _showView;
@synthesize portraitImageView   = _portraitImageView;
@synthesize timeStatusLabel           = _timeStatusLabel;
@synthesize locationLabel = _locationLabel;
@synthesize numberLabel = _numberLabel;
#pragma mark - Override

- (void)setMarkerSize:(CGSize)markerSize{
    _markerSize = markerSize;
    _markerImageView.size = markerSize;
}

- (void)setMarkerImage:(NSString *)markerImage{
    _markerImage = markerImage;
    _markerImageView.image = [UIImage imageNamed:markerImage];
}

- (void)setShowBottomTitle:(BOOL)showBottomTitle{
    
    _showBottomTitle = showBottomTitle;
    self.showView.hidden = !_showBottomTitle;
    self.timeStatusLabel.hidden = !_showBottomTitle;
    self.numberLabel.hidden = !_showBottomTitle;
    self.locationLabel.hidden = !_showBottomTitle;
}
-(NSString *)timeStr{
    return self.timeStatusLabel.text;
}
-(void)setTimeStr:(NSString *)timeStr{
    self.timeStatusLabel.text = timeStr;
    CGFloat width = [self boundingRectWithString:self.timeStatusLabel.text font:self.timeStatusLabel.font].width + kVertMargin;
    self.timeStatusLabel.width = width;
    self.timeStatusLabel.x = - width /2.0 + kVertMargin;
}
-(NSString *)locationStr{
    return self.locationLabel.text;
}
-(void)setLocationStr:(NSString *)locationStr{
    self.locationLabel.text = locationStr;
    
    CGFloat width = [self boundingRectWithString:self.locationLabel.text font:self.locationLabel.font].width + kVertMargin;
    self.locationLabel.width = width;
    self.locationLabel.x = - width /2.0 + CGRectGetMaxX(self.timeStatusLabel.frame);
}
-(NSString *)numberStr{
    return self.numberLabel.text;
}
-(void)setNumberStr:(NSString *)numberStr{
    self.numberLabel.text = numberStr;
    
    CGFloat width = [self boundingRectWithString:self.numberLabel.text font:self.locationLabel.font].width + kVertMargin * 2;
    self.numberLabel.width = width;
    self.numberLabel.x = - width /2.0 + CGRectGetMaxX(self.timeStatusLabel.frame);
    
    CGFloat maxWidth = self.locationLabel.width > self.numberLabel.width ? self.locationLabel.width : self.numberLabel.width;
    CGFloat calloutViewWidth = 55 + maxWidth;
    CGFloat calloutViewHeight = 41;
    CGFloat x = -calloutViewWidth / 2 + 10;
    self.showView.frame  =CGRectMake(x, kDiameter, calloutViewWidth, calloutViewHeight);
//    [self layoutIfNeeded];
}
//-(void)layoutIfNeeded{
//    [super layoutIfNeeded];
//    CGFloat maxWidth = self.locationLabel.width > self.numberLabel.width ? self.locationLabel.width : self.numberLabel.width;
//    CGFloat calloutViewWidth = 55 + maxWidth;
//    CGFloat calloutViewHeight = 41;
//    CGFloat x = -calloutViewWidth / 2 + 10;
//    self.showView.frame  =CGRectMake(x, kDiameter, calloutViewWidth, calloutViewHeight);
//}
- (UIImage *)portrait
{
    return self.portraitImageView.image;
}

- (void)setPortrait:(UIImage *)portrait
{
    self.portraitImageView.image = portrait;
}

- (void)setSelected:(BOOL)selected
{
    [self setSelected:selected animated:NO];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    if (self.selected == selected)
    {
        return;
    }
    
    if (selected)
    {
        if (self.calloutView == nil)
        {
            CGSize timeSize = [NSString boundingRectWithString:self.timeStr font:[UIFont systemFontOfSize:kFontSize]];
            CGSize locationSize = [NSString boundingRectWithString:self.locationStr font:[UIFont systemFontOfSize:kFontSize]];
            CGSize numberSize = [NSString boundingRectWithString:self.numberStr font:[UIFont systemFontOfSize:kFontSize]];
    
            CGFloat calloutViewWidth = timeSize.width + locationSize.width + 26;
            CGFloat calloutViewHeight = locationSize.height + numberSize.height + 25;
            /* Construct custom callout. */
            self.calloutView = [[CustomCalloutView alloc] initWithFrame:CGRectMake(0, 0, calloutViewWidth, calloutViewHeight)];
            self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,-CGRectGetHeight(self.calloutView.bounds) / 2.f + self.calloutOffset.y);
            
            UILabel *time = [[UILabel alloc] initWithFrame:CGRectMake(5, calloutViewHeight/2 - 6, timeSize.width, 12)];
            time.textColor = SUB_TITLE_COLOR;
            time.font = [UIFont systemFontOfSize:kFontSize];
            time.text = self.timeStr;
            [self.calloutView addSubview:time];
            
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(time.frame)+ 5,  5, .5,calloutViewHeight - 15)];
            imageView.image = [UIImage imageNamed:@"separator"];
            [self.calloutView addSubview:imageView];
            
            UILabel *location = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame)+5, 7, locationSize.width, locationSize.height)];
            location.textColor = BLUE_COLOR;
            location.font = [UIFont systemFontOfSize:kFontSize];
            location.text = self.locationStr;
            [self.calloutView addSubview:location];
            
            UILabel *number = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame)+5, CGRectGetMaxY(location.frame)+5, numberSize.width, numberSize.height)];
            number.textColor = BLUE_COLOR;
            number.font = [UIFont systemFontOfSize:kFontSize];
            number.text = self.numberStr;
            [self.calloutView addSubview:number];
        }
        
        [self addSubview:self.calloutView];
    }
    else
    {
        [self.calloutView removeFromSuperview];
    }
    
    [super setSelected:selected animated:animated];
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    BOOL inside = [super pointInside:point withEvent:event];
    /* Points that lie outside the receiver’s bounds are never reported as hits,
     even if they actually lie within one of the receiver’s subviews.
     This can occur if the current view’s clipsToBounds property is set to NO and the affected subview extends beyond the view’s bounds.
     */
    if (!inside && self.isClick)
    {
        inside = [self.calloutView pointInside:[self convertPoint:point toView:self.calloutView] withEvent:event];
    }
    
    return inside;
}

- (void)setIsClick:(BOOL)isClick{
    _isClick = isClick;
}

- (void)setPointAnnotation:(YMUserPointAnnotation *)pointAnnotation{
      _pointAnnotation = pointAnnotation;
    
    self.timeStr = pointAnnotation.timeStr;
    self.locationStr = pointAnnotation.locationStr;
    self.numberStr = pointAnnotation.numberStr;
    
    [self setNeedsDisplay];
}

#pragma mark - Life Cycle
- (id)initWithAnnotation:(id<MAAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        self.bounds = CGRectMake(0.f, 0.f, kDiameter, kDiameter);
        
        _markerImageView = [[UIImageView alloc] initWithFrame:self.bounds];
        _markerImageView.image = [UIImage imageNamed:@"icon_map_pathway_site"];
        
        [self addSubview:_markerImageView];
        
        /* Create portrait image view and add to view hierarchy. */
        self.portraitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kHoriMargin, kVertMargin, kPortraitWidth, kPortraitHeight)];
        [self addSubview:self.portraitImageView];
        
        CGFloat calloutViewWidth = kWidth;
        CGFloat calloutViewHeight = 41;
        CGFloat x = -calloutViewWidth / 2;
        CGFloat y = kDiameter;
        
        self.showView = [[UIView alloc]initWithFrame:CGRectMake(x, y, calloutViewWidth, calloutViewHeight)];
        self.showView.backgroundColor = WHITE_COLOR;
        self.showView.layer.cornerRadius = 5.0;
        self.showView.layer.borderWidth = 1.0;
        self.showView.layer.borderColor = SEPARATE_LINE_COLOR.CGColor;
        [self addSubview:self.showView];
        
        self.timeStatusLabel = [[UILabel alloc] init];
        self.timeStatusLabel.textAlignment    = NSTextAlignmentCenter;
        self.timeStatusLabel.textColor        = SUB_TITLE_COLOR;
        self.timeStatusLabel.font             = [UIFont systemFontOfSize:kFontSize];
        [self.showView addSubview:self.timeStatusLabel];
        [self.timeStatusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(12));
            make.width.equalTo(@(33));
            make.left.equalTo(self.showView.mas_left).offset(5);
            make.centerY.equalTo(self.showView.mas_centerY);
        }];
        self.separateImageView = [[UIImageView alloc]init];
        self.separateImageView.image  = [UIImage imageNamed:@"separator"];
        [self.showView addSubview:self.separateImageView];
        [self.separateImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(.5));
            make.top.equalTo(self.showView.mas_top).offset(5);
            make.left.equalTo(self.timeStatusLabel.mas_right).offset(5);
            make.bottom.equalTo(self.showView.mas_bottom).offset(-5);
        }];
        
        self.locationLabel = [[UILabel alloc] init];
        self.locationLabel.textAlignment    = NSTextAlignmentLeft;
        self.locationLabel.textColor        = BLUE_COLOR;
        self.locationLabel.font             = [UIFont systemFontOfSize:kFontSize];
        [self.showView addSubview:self.locationLabel];
        [self.locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.separateImageView.mas_right).offset(5);
            make.top.equalTo(self.showView.mas_top).offset(7);
            make.right.equalTo(self.showView.mas_right).offset(-5);
            make.height.equalTo(@(12));
        }];
        self.numberLabel = [[UILabel alloc] init];
        self.numberLabel.textAlignment    = NSTextAlignmentLeft;
        self.numberLabel.textColor        = BLUE_COLOR;
        self.numberLabel.font             = [UIFont systemFontOfSize:kFontSize];
        
        [self.showView addSubview:self.numberLabel];
        [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(12));
            make.top.equalTo(self.locationLabel.mas_bottom).offset(5);
            make.left.equalTo(self.locationLabel.mas_left);
        }];
    }
    
    return self;
}
#pragma mark---Handle
// 根据字体计算字符串长度
- (CGSize)boundingRectWithString:(NSString *)string font:(UIFont *)font{
    
    NSMutableDictionary * attr = [NSMutableDictionary dictionary];
    
    attr[NSFontAttributeName] = font;
    
    CGSize maxSize = CGSizeMake(MAXFLOAT, MAXFLOAT);
    
    CGSize size = [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size;
    
    return size;
}
@end

